// ml:run = $bin < input
// ml:opt = 0
// ml:ccf += -g
#include <iostream>
#include <vector>

using ll = long long;
ll const mo = 1000000007;
int const maxn = 200007;
int vis[maxn];
int dep[maxn];
int a[maxn];
int n, tick;
std::vector<int> circle;

ll quick(int x)
{
    if (!x) return 1;
    auto tmp = quick(x / 2);
    tmp = (tmp * tmp) % mo;
    if (x & 1) tmp = (tmp * 2) % mo;
    return tmp;
}

void dfs(int u, int l = 0)
{
    vis[u] = tick;
    dep[u] = l;
    if (vis[a[u]] == tick) {
        circle.push_back(l - dep[a[u]] + 1);
    } else if (!vis[a[u]])
        dfs(a[u], l + 1);
}

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin >> n;
    for (int i = 1; i <= n; i++) std::cin >> a[i];
    for (int i = 1; i <= n; i++) {
        if (vis[i]) continue;
        tick++;
        dfs(i);
    }
    int sum = 0;
    for (auto i : circle) sum += i;
    ll ans = quick(n - sum);
    for (auto i : circle) ans = (ans * (quick(i) - 2)) % mo;
    std::cout << ans << "\n";
}

